package com.example.skycode.servlet;

import com.example.skycode.bean.news;
import com.example.skycode.dao.NewsDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;

@WebServlet("/newsAdd.jsp")
public class NewsAddServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       req.setCharacterEncoding("UTF-8");
       String newsTitle = req.getParameter("NewsTitle");
       String newsContent = req.getParameter("NewsContent");
        news news = new news();
        //设置新闻对象的属性值
        news.setNewsTitle(newsTitle);
        news.setNewsContent(newsContent);
        news.setNewsTime( (new Date()).toString()   );//当前系统时间
        news.setAdminName("admin");

        NewsDao newsDao = new NewsDao();
        int result = newsDao.add(news);
        if (result>0){
            //补充知识 跳转到网页news.html中
            resp.sendRedirect("news.html");//使用结果对象 完成页面网址跳转
        }else {//失败显示错误提是页面
           req.getRequestDispatcher("WEB-INF/error.jsp").forward(req,resp);
        }

    }
}
